package com.android.sms_manager;

import com.android.sms_manager.data.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;



public class ListSmsActivity extends Activity {
	
	private static final int MENU_DELETE = 1;
	private String title = null;
	private String uri = null;
	private int protocol;
	private int thread_id;
	private int type;
	private String num;
	private int id;
	private MySmsAdapter adapter = null;
	private ListView smsList = null;
	private TextView tvSend;
	private Button buttonSend;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loadIntentData();
		
		setContentView(R.layout.sendsms);
		smsList = (ListView) this.findViewById(R.id.smslist);
		tvSend = (TextView) this.findViewById(R.id.text_send);
		buttonSend = (Button) this.findViewById(R.id.button_send);
		smsList.setSmoothScrollbarEnabled(true);
	
		loadData();		
		
		this.registerForContextMenu(smsList);
		registerListener();
	
	}

	private void loadIntentData() {
		Intent intent = this.getIntent();
		title = intent.getStringExtra(MyData.KEY_TITLE);
		uri = intent.getStringExtra(MyData.KEY_URI);
		protocol = intent.getIntExtra(MyData.KEY_PROTOCOL, MyData.PROTOCOL_SMS);
		thread_id = intent.getIntExtra(MyData.KEY_THREAD_ID, 0);
		type = intent.getIntExtra(MyData.KEY_MSG_BOX, MyData.TYPE_ALL);
		num = intent.getStringExtra(MyData.KEY_ADDRESS);
		id = intent.getIntExtra(MyData.KEY_ID, 0);
	//	Toast.makeText(ListSmsActivity.this,id + "id", Toast.LENGTH_SHORT).show();
	}

	private void loadData() {
		
		adapter = new MySmsAdapter(this, thread_id, protocol, uri, id);
		smsList.setAdapter(adapter);
		this.setTitle(title + " ( " + adapter.getCount() + " )");
		if(id != 0)
			smsList.setSelection(adapter.getposition(id));
		else 
			smsList.setSelection(adapter.getCount()-1);
//		int i = adapter.getposition(id);
//		adapter.setSelectItem(i);
//		adapter.notifyAll();
//		smsList.getChildAt(adapter.getposition(id)-smsList.getFirstVisiblePosition()).setBackgroundColor(color.white);
//		smsList.getSelectedView().setBackgroundColor(color.white);


	}
	
	private void registerListener(){
		buttonSend.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
	    			    	
	    		String message = tvSend.getText().toString();

	    		SmsManager sms = SmsManager.getDefault();
	            sms.sendTextMessage(num, null, message, null, null);
	           
	    		tvSend.setText(null);
	    		Toast.makeText(getApplicationContext(), num+","+message, Toast.LENGTH_SHORT).show();
	    		//refrash the data
	    		loadData();
	    	
			}
			
		});
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// add long click menu item
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, MENU_DELETE, 0, R.string.delete);
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// when menu is selected
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
				.getMenuInfo();
		final int position = menuInfo.position;
		switch (item.getItemId()) {
		case MENU_DELETE:
			new AlertDialog.Builder(this)
					.setTitle(R.string.ifdelete)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface arg0,
										int arg1) {								
									//Do delete
									adapter.deleteItem(position, type);
									loadData();
									
									Log.i("delete", "positon = "+position);
								}
							})
					.setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface arg0,
										int arg1) {								
									// Do nothing
								}
							}).create().show();
			break;

		default:
			break;
		}

		return super.onContextItemSelected(item);
	}
	
}
